package com.sg.service.base.operation.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.operation.req.QueryBlockchainCertificationListReq;
import com.sg.entity.OmsOperationBlockchainCertificate;
import com.sg.service.base.operation.MOmsOperationBlockchainCertificateService;
import com.wicket.okroperation.common.dto.FieldInfo;
import com.wicket.okroperation.common.exception.BizException;
import com.wicket.okroperation.dal.mapper.mbg.OmsOperationBlockchainCertificateMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsOperationBlockchainCertificateServiceImpl
        extends ServiceImpl<OmsOperationBlockchainCertificateMapper, OmsOperationBlockchainCertificate>
        implements MOmsOperationBlockchainCertificateService {

    @Autowired
    OmsOperationBlockchainCertificateMapper omsOperationBlockchainCertificateMapper;

    /**
     * 6-1-02新增区块链证书[6992]
     * gen by moon at 5/30/2023, 6:57:12 PM
     */
    @Trace(operationName = "6-1-02新增区块链证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBlockchainCertification(OmsOperationBlockchainCertificate omsOperationBlockchainCertificate) {
        omsOperationBlockchainCertificate.setBlockchainCertificateId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsOperationBlockchainCertificate.setIsArchive("FALSE");
        omsOperationBlockchainCertificate.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsOperationBlockchainCertificate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsOperationBlockchainCertificate.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsOperationBlockchainCertificate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsOperationBlockchainCertificate.setIsValid("FALSE");
        omsOperationBlockchainCertificate.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsOperationBlockchainCertificate.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        if (super.save(omsOperationBlockchainCertificate)) {
            return omsOperationBlockchainCertificate.getBlockchainCertificateId();
        } else {
            return "-1";
        }

    }

    /**
     * 6-1-02查询区块链证书列表[6994]
     * gen by moon at 5/30/2023, 6:57:15 PM
     */
    @Trace(operationName = "6-1-02查询区块链证书列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsOperationBlockchainCertificate> queryBlockchainCertificationList(QueryBlockchainCertificationListReq reqDto) {

        try {
            QueryWrapper<OmsOperationBlockchainCertificate> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsOperationBlockchainCertificate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsOperationBlockchainCertificate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsOperationBlockchainCertificate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsOperationBlockchainCertificate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 6-1-02查询区块链证书详情[6995]
     * gen by moon at 5/30/2023, 6:57:16 PM
     */
    @Trace(operationName = "6-1-02查询区块链证书详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsOperationBlockchainCertificate queryBlockchainCertificationDetail(OmsOperationBlockchainCertificate omsOperationBlockchainCertificate) {

        try {

            QueryWrapper<OmsOperationBlockchainCertificate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsOperationBlockchainCertificate);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsOperationBlockchainCertificate> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsOperationBlockchainCertificate retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 6-1-02修改区块链证书[6996]
     * gen by moon at 5/30/2023, 6:57:16 PM
     */
    @Trace(operationName = "6-1-02修改区块链证书")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBlockchainCertification(OmsOperationBlockchainCertificate omsOperationBlockchainCertificate) {
        omsOperationBlockchainCertificate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsOperationBlockchainCertificate != null && omsOperationBlockchainCertificate.getSubjectLifeCycle() != null && omsOperationBlockchainCertificate.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsOperationBlockchainCertificate.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsOperationBlockchainCertificate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsOperationBlockchainCertificate != null && omsOperationBlockchainCertificate.getSubjectLifeCycle() != null && omsOperationBlockchainCertificate.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsOperationBlockchainCertificate.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsOperationBlockchainCertificate);
    }
}
